package com.gooluke.netty;

import com.gooluke.netty.handler.MyServerChannelInboundHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author gooluke
 * description
 * datetime 2025-05-18 3:20
 */
public class NettyServer {

    public static void main(String[] args) {
        ServerBootstrap serverBootstrap = new ServerBootstrap().group(new NioEventLoopGroup(1),new NioEventLoopGroup(8));
        serverBootstrap.channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline()
//                                .addLast(new LineBasedFrameDecoder(1024))
                                .addLast(new StringEncoder())
                                .addLast(new StringDecoder())
                                .addLast(new MyServerChannelInboundHandler());
                    }
                });
        ChannelFuture channelFuture = serverBootstrap.bind(9000);
        channelFuture.addListener(future -> {
            if (future.isSuccess()) {
                System.out.println("启动成功，监听9000端口");
            }
        });

    }

}
